package com.neusoft.elmboot.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.elmboot.mapper.OrdersMapper;
import com.neusoft.elmboot.mapper.PointMapper;
import com.neusoft.elmboot.mapper.TransactionMapper;
import com.neusoft.elmboot.mapper.VirtualWalletMapper;
import com.neusoft.elmboot.po.Record;
import com.neusoft.elmboot.po.VirtualWallet;
import com.neusoft.elmboot.service.OrdersService;
import com.neusoft.elmboot.service.VirtualWalletService;

@Service
public class VirtualWalletServiceImpl implements VirtualWalletService {
	
	@Autowired
	private VirtualWalletMapper walletmapper;
	
	@Autowired
	private TransactionMapper transactionmapper;
	
	@Autowired
	private OrdersMapper ordersmapper;
	
	@Autowired PointMapper pointmapper;
	
    public BigDecimal getBalance(String userId) {
    	Long walletId = walletmapper.getWalletIdByUserId(userId);
        return walletmapper.getBalanceByWalletId(walletId); 
    }   
    
    public int RechargeOrWithdraw(String userId,String password,BigDecimal amount) {
    	Long walletId = walletmapper.getWalletIdByUserId(userId);
    	if (BigDecimal.ZERO.compareTo(amount) > 0) {   
             debit(walletId,BigDecimal.ZERO.subtract(amount));
        } else {
        	credit(walletId,amount);
        }
    	return 1;
    }
    
    @Transactional
    public void debit(Long walletId, BigDecimal amount) {  
        VirtualWallet wallet = new VirtualWallet(walletId,walletmapper.getBalanceByWalletId(walletId));
        wallet.debit(amount);
        walletmapper.updateBalance(walletId, wallet.balance());  
        transactionmapper.saveTrans(amount, 0, walletId);
    } 
    
    @Transactional
    public void credit(Long walletId, BigDecimal amount) {
    	VirtualWallet wallet = new VirtualWallet(walletId,walletmapper.getBalanceByWalletId(walletId));    
        wallet.credit(amount);    
        walletmapper.updateBalance(walletId, wallet.balance());  
        transactionmapper.saveTrans(amount, 1, walletId);
    }

	@Override
	public List<Record> getTransactionRecordByUserId(String userId) {
		Long walletId = walletmapper.getWalletIdByUserId(userId);
		return transactionmapper.getTransBywalletId(walletId);
	}

	@Override
	@Transactional
	public int payByOrderId(String userId , String password, int orderId) {
		// password have not used in the process
		
		Double ordertotal = ordersmapper.getOrdersById(orderId).getOrderTotal();
		Long walletId = walletmapper.getWalletIdByUserId(userId);
		BigDecimal balance = walletmapper.getBalanceByWalletId(walletId);
		
		if(balance.compareTo(BigDecimal.valueOf(ordertotal))<0) {
			return -1;
		}
		
		//pay record
		VirtualWallet wallet = new VirtualWallet(walletId,walletmapper.getBalanceByWalletId(walletId));
        wallet.debit(BigDecimal.valueOf(ordertotal));
        walletmapper.updateBalance(walletId, wallet.balance());  
        transactionmapper.saveTrans(BigDecimal.valueOf(ordertotal), 2 , walletId);
		
		return ordersmapper.pay(orderId);
	}
	
}
